import { getUserInfoApi } from '@/api/login'
import { USER } from '../types'
import { getToken } from '@/utils/auth'
const state = {
    // 用户信息
    userInfo: {}
}

const mutations = {
    // 更新用户信息
    updateUserInfo(state, payload) {
        state.userInfo = Object.assign({}, state.userInfo, payload)
    }
}

const actions = {
    getUserInfoFunc: async ({ commit }) => {
        const token = getToken()
        if(token){
            const data: any = await getUserInfoApi()
            if(data){
                const result = <USER>{
                    userId: data.userId,
                    userName: data.userName,
                    roles: data.roles
                }
                commit('updateUserInfo', result)
            }
        }
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
